package nio;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author ilongsay
 * @date: Create in 2020-03-06 9:02
 * @description: don't bb,show you code
 */
public class IOServer {
    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();

        ServerSocket serverSocket = new ServerSocket(8088);
        System.out.println("服务启动了");
        while (true) {
            final Socket socket = serverSocket.accept();
            executorService.execute(()-> handler(socket));
        }
    }

    public static void handler(Socket socket) {
        System.out.println("================");
        System.out.println("================");
        System.out.println("================");
        System.out.println("================");
        System.out.println(Thread.currentThread().getName()+"   "+Thread.currentThread().getId());
        byte[] bytes = new byte[1024];
        try (InputStream inputStream = socket.getInputStream()) {
            int read = inputStream.read(bytes);
            while (true) {
                if (read != -1) {
                    System.out.println(new String(bytes, 0, read));
                } else {
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
